﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MicroBenchmarks.SideEffects
{
    /// <summary>
    /// A class that demonstrates the challenges where a desired object state is created 
    /// by a desired combination of methods. This has side effects where when of the methods
    /// is invoked, it is going to require some other desired state from another class, making the automaton more complex
    /// 
    /// The class has an implicit protocol imposed by SimpleStack that IncrM3AndDecrM1 should not be called
    /// more number of times than IncrM2AndDecrM1, if not ss.pop() will throw exception
    /// </summary>
    public class InternalFields
    {
        private int member1, member2, member3;
        private bool localState;
        
        private SimpleStack ss;

        /// <summary>
        /// Default constructor which is by default invoked 
        /// </summary>
        public InternalFields()
        {
        }

        /// <summary>
        /// A constructor that needs to be invoked after detecting that simple stack is required
        /// </summary>
        /// <param name="ss"></param>
        public InternalFields(SimpleStack ss)
        {
            this.ss = ss;
        }
        
        public bool InternalState 
        {
            get{
                return localState;
            }        
        }

        private void SetLocalState()
        {
            if (member1 == 5 && member2 == 3 && member3 > 2)
                localState = true;
        }

        public void IncrM1()
        {
            member1++;
            SetLocalState();
        }

        public void IncrM2AndDecrM1()
        {            
            member2++;
            member1--;
            ss.Push(member2);
            SetLocalState();
        }

        public void IncrM3AndDecrM1()
        {
            member3++;
            member1--;
            ss.Pop();
            SetLocalState();
        }
    }
}
